<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            height: 100%;
            background-color: #ccc;
            position: relative;
        }

        #cart {
            width: 100%;
            position: fixed;
            bottom: 0;
            background-color: white;
            padding: 0 15px;
        }

        #cart .title {
            line-height: 50px;
            display: flex;
            padding-right: 25px;
            justify-content: space-between;
        }

        #cart .title .t-left {
            font-size: 18px;
            font-weight: 700;
        }

        #cart .item {
            margin: 5px 0;
            border-radius: 10px;
            display: flex;
            position: relative;
        }

        #cart .item .center {
            height: 100%;
            margin-left: 10px;
        }

        #cart .item .center .name {
            font-size: 14px;
            font-weight: 700;
        }

        #cart .item .center .much {
            font-size: 14px;
            margin-top: 25px;
            color: red;
        }

        #cart .item .right {
            float: right;
            display: flex;
            position: absolute;
            right: 30px;
            bottom: 0;
        }

        #cart .item .right button {
            width: 20px;
            height: 20px;
            border-radius: 50%;
        }

        #cart .item .right .num {
            margin: 0 10px;
        }

        #cart .item .right button.b1 {
            color: skyblue;
            border: 1px solid skyblue;
            background-color: white;
        }

        #cart .item .right button.b2 {
            color: white;
            border: none;
            background-color: skyblue;
        }

        .null {
            color: red;
            text-align: center;
            margin: 30px 0 60px 0;
        }

        #cart .add {
            width: 100%;
            line-height: 50px;
            position: absolute;
            bottom: 0;
            background-color: rgba(128, 128, 128, 0.921);
            display: flex;
            justify-content: space-around;
        }

        #cart .add button {
            width: 80px;
            height: 35px;
            background-color: rgb(127, 204, 10);
            border: none;
            color: white;
            margin-top: 5px;
            border-radius: 5px;
        }
    </style>
</head>

<body>
    <div id="cart">
        <div class="title">
            <div class="t-left">已选商品({{allnum+goodslist.length}})</div>
            <div class="t-left" @click="del()">🚽清空</div>
        </div>
        <div class="item" v-for="(item,index) in goodslist">
            <div class="left">
                <img v-bind:src="item.pic" alt="">
            </div>
            <div class="center">
                <div class="name">{{item.name}}</div>
                <div class="much">￥{{item.much}}.00</div>
            </div>
            <div class="right">
                <button class="b1" @click="sub(item)">－</button>
                <div class="num">{{item.num}}</div>
                <button class="b2" @click="add(item)">＋</button>
            </div>
        </div>
        <div class="null" v-show="goodslist.length == 0">购物车空空如也！</div>
        <div class="add">
            <div>合计：{{money()}} 元<div class="money"></div>
            </div>
            <button>去结算</button>
        </div>
    </div>
</body>

</html>
<script src="./vue-2.5.21.js"></script>
<script>

    new Vue({
        el: '#cart',
        data: {
            allnum: 0,
            goodslist: [
                { id: 1, pic: './img/fs1.png', name: '秋刀鱼/斤', much: 35, num: 1 },
                { id: 2, pic: './img/fs2.png', name: '鲜大带鱼/条', much: 45, num: 1 },
                { id: 3, pic: './img/fs3.png', name: '鲜活全母皮皮虾/斤', much: 85, num: 1 },
                { id: 4, pic: './img/fs4.png', name: '13-15头鲍鱼/只', much: 10, num: 1 },
            ]
        },
        methods: {
            money() {
                var money = 0;
                this.goodslist.forEach((item, index) => {
                    money += item.much * item.num;
                });
                return money;
            },
            add(item) {
                if (item.num < 5) {
                    item.num += 1;
                    this.allnum += 1;
                }
            },
            sub(item) {
                if (item.num > 1) {
                    item.num -= 1;
                    this.allnum -= 1;
                }
            },
            del() {
                this.goodslist.splice(0, this.goodslist.length);
            }
        }
    })

</script>